package gojs;

import com.alibaba.fastjson.JSON;
import common.AsyncFlowNodeGoJs;
import common.mongodb.MongoHelper;

import models.AsyncFlowLogEntity;
import models.AsyncFlowNodeLogEntity;
import org.bson.Document;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("diagram")
public class DiagramDivController {

    @RequestMapping("flowDiagram")
    public String hello(Model model) {
        MongoHelper<AsyncFlowLogEntity> mongoHelper;
        mongoHelper = new MongoHelper<>("mongodb://localhost:27017", "TCGnyProduct", "AsyncFlowLog");
        // mongoHelper = new MongoHelper<>("mongodb://tcgnyproduct:ydsWD7dIz8%25Dsid3tr9@172.18.4.30:10001,172.18.4.31:10001,172.18.4.33:10001/TCGnyProduct?replicaSet=tctourism&amp;readPreference=secondary", "TCGnyProduct", "AsyncFlowLog");
        Document asyncFlowLogDocument = mongoHelper.findById("60853c8ac6777b8db0fa33c6");
        AsyncFlowLogEntity appInfo = JSON.parseObject(asyncFlowLogDocument.toJson(), AsyncFlowLogEntity.class);

        for (int i = 0; i < 4; i++) {
            AsyncFlowNodeLogEntity taskInfo = new AsyncFlowNodeLogEntity();
            taskInfo.setLogFileName("Common_CallOrderWorkFlowAsync" + i);
            taskInfo.setLogMethodName(String.valueOf(i));
            taskInfo.setAsync(true);
            if (i == 3) {
                taskInfo.setAsync(false);
            }
            appInfo.getAsyncFlowNodeLogEntityList().add(taskInfo);
        }
        model.addAttribute("asyncFlowNodeDataArray", AsyncFlowNodeGoJs.getAllNodes(appInfo.getAsyncFlowNodeLogEntityList()));
        model.addAttribute("asyncFlowNodeLinkData", AsyncFlowNodeGoJs.getLinkDataArray(appInfo.getAsyncFlowNodeLogEntityList()));
        return "myDiagramDiv";
    }


}
